package cn.tedu.ivos.user.controller;

import cn.tedu.ivos.base.response.JsonResult;
import cn.tedu.ivos.user.pojo.dto.UserLoginParam;
import cn.tedu.ivos.user.pojo.dto.UserQuery;
import cn.tedu.ivos.user.pojo.dto.UserSaveParam;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/user")
public class UserController {
    @Autowired
    UserService userService;

    /**
     * 用户登录
     * POST http://localhost:8080/v1/user/login?username=admin&password=admin
     * @param userLoginParam
     * @return
     */
    @PostMapping("login")
    public JsonResult login(UserLoginParam userLoginParam){
        UserVO userVO = userService.login(userLoginParam);
        return JsonResult.ok(userVO);
    }
    /**
     * 新增用户
     * POST http://localhost:8080/v1/user/save?username=kk&password=kk&email=kk&phone=kk&age=18&gender=1&status=1&level=10&parentId=6
     * 修改用户
     * POST http://localhost:8080/v1/user/save?id=20&email=kk2&phone=kk2&age=20&gender=0&status=0&level=20&parentId=3
     * @param userSaveParam
     * @return
     */
    @PostMapping("save")
    public JsonResult saveUser(UserSaveParam userSaveParam){
        log.debug("保存用户,userSaveParam={}",userSaveParam);
        userService.saveUser(userSaveParam);
        return JsonResult.ok();
    }

    /**
     * 查询用户方法
     * @param userQuery
     * @return
     */
    @GetMapping("select")
    public JsonResult selectUser(UserQuery userQuery){
        log.debug("查询用户:userQuery={}",userQuery);
        List<UserVO> list = userService.selectUser(userQuery);
        return JsonResult.ok(list);
    }

    @PostMapping("/update/password/{userId}")
    public JsonResult resetPassword(@PathVariable Long userId){
        log.debug("重置密码,userId={}",userId);
        userService.resetPassword(userId);
        return JsonResult.ok();
    }
    @PostMapping("/update/status/{userId}/{status}")
    public JsonResult updateStatus(@PathVariable Long userId,@PathVariable String status){
        log.debug("修改用户状态,userId={},status={}",userId,status);
        userService.updateStatus(userId,status);
        return JsonResult.ok();
    }

    @PostMapping("/delete/{userId}")
    public JsonResult deleteUser(@PathVariable Long userId){
        log.debug("删除用户,userId={}",userId);
        userService.deleteUser(userId);
        return JsonResult.ok();
    }

    @GetMapping("/select/audit/{parentId}")
    public JsonResult selectAuditList(@PathVariable Long parentId){
        log.debug("查询上级审批人列表parentId = " + parentId);
        List<UserVO> list = userService.selectAuditList(parentId);
        return JsonResult.ok(list);
    }
}